<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2018/11/28
 * Time: 11:43
 */

namespace App\Models;


use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class TaskCount extends Model
{
    protected $table = 'task_count';
    protected $fillable = [
       'id','user_id','watch','shared_group','shared_friends','sign_in','video_zans','video_collects','invite_friends','created_at','updated_at'
    ];
//    public $timestamps = false;//关闭自动维护

    public $dates = ['created_at','updated_at'];

    /**
     * 更新任务统计表
     * @param int $uid
     * @param string $type
     */
    public function

    updateCount($uid = 0,$type = ''){
        $findTask = $this->where('user_id',$uid)->where('created_at','>=' ,Carbon::today())->first();
        if(!$type){
            return false;
        }
        if($findTask){
            $configNumber = config('task.maximum_times.'.$type);
            if($configNumber > $findTask[$type]){
                if ($type !== 'sign_in'){
                    $findTask->increment($type);
                }elseif($findTask['sign_in'] === 0){
                    $findTask->update(['sign_in'=>1]);
                }
            }else{
                return false;
            }
        }else{
            $data['user_id'] = $uid;
            $data['created_at'] = Carbon::now();
            $data[$type] = 1;
            $findTask = $this->create($data);
        }
        return $findTask;
    }
}